package com.lcxw.hxzbadmin.controller.avatarcontroller;

import com.lcxw.hxzbadmin.entity.requestbean.AddAvatarReq;
import com.lcxw.hxzbadmin.entity.requestbean.GetAllAvatarReq;
import com.lcxw.hxzbadmin.entity.requestbean.ModAvatarReq;
import com.lcxw.hxzbadmin.entity.requestbean.UploadAvatarPicReq;
import com.lcxw.hxzbapi.entity.pojo.Avatar;
import com.lcxw.hxzbapi.entity.response.Response;
import com.lcxw.hxzbapi.service.AvatarService;
import com.lcxw.hxzbcommon.common.ObsUtil;
import com.lcxw.hxzbcommon.entity.ErrorCode;
import javax.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("admin/avatar")
public class AvatarController {

    @Resource
    AvatarService avatarService;

    private ObsUtil obsUtil = new ObsUtil();

    /**
     * 上传头像框图片接口
     */
    @PostMapping("/pic/upload")
    public Response uploadAvatarPic(@RequestBody UploadAvatarPicReq uploadAvatarPicReq) {
        String imgName = uploadAvatarPicReq.getImgName();
        String imgOrigin = uploadAvatarPicReq.getImgOrigin();
        imgName = "avatar/" + imgName;
        try {
            String url = obsUtil.uploadPic(imgName, imgOrigin);
            return Response.ok(url);
        } catch (Exception e) {
            return Response.error(ErrorCode.EXTERNAL_ERROR, "上传到obs失败");
        }
    }

    /**
     * 获取所有头像框
     */
    @PostMapping("/getAll")
    public Response getAllAvatar(@RequestBody GetAllAvatarReq requestBodyBasic) {
        String name = requestBodyBasic.getName();
        Integer status = requestBodyBasic.getStatus();
        int pageNum = requestBodyBasic.getPageNum();
        int pageSize = requestBodyBasic.getPageSize();
        return avatarService.getAll(name, status, pageNum, pageSize);
    }

    /**
     * 添加一个头像框
     */
    @PostMapping("/add")
    public Response addAvatar(@RequestBody AddAvatarReq addAvatarReq) {
        Avatar avatar = new Avatar();
        BeanUtils.copyProperties(addAvatarReq, avatar);
        avatar.setStatus(1);
        avatarService.save(avatar);
        return Response.ok();
    }

    /**
     * 修改头像框
     */
    @PostMapping("mod")
    public Response modAvatar(@RequestBody ModAvatarReq modAvatarReq) {
        int avatarId = modAvatarReq.getAvatarId();
        Avatar avatar = avatarService.getById(avatarId);
        BeanUtils.copyProperties(modAvatarReq, avatar);
        avatarService.updateById(avatar);
        return Response.ok();
    }

}
